<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/9/15
 * Time: 9:33
 * 新闻表控制器：1、运用前端提供的新闻详情的模板，添加一篇新闻入库后，操作模板文件完成替换生成详情页的文件
 * 2、查询数据库显示新闻列表页
 * 3、更新新闻后，更新对应文件
 * 4、删除新闻后，删除对应文件
 * 运用封装好的 Cache_file.class.php
 */
class newsAction extends Action{
    const NEWS_LIST_URL = "index.php?act=news&op=list";
    public function addop(){
        if(isset($_POST['submit_news_add'])){
           $insert_array ['news_title']=  $_POST['news_title'];
           $insert_array ['news_contents']=  $_POST['news_contents'];
           //Cache_file.class.php，实例化该类的对象，调用添加入库并生成文件的方法
            $cache_file_obj = new Cache_file('news');
           // T($cache_file_obj);
            if($cache_file_obj->addRecordAndFile('news',$insert_array,'news_detail_tpl.php','news_file_detail_url')){
               $this->showMessage('添加新闻及生成静态文件成功',self::NEWS_LIST_URL,1);
            }else{
                echo $cache_file_obj->error;
            }

        }else{

            include_once TEMPLATES_DIR.DS."news_add.php";
        }
    }
    public function listop(){
        $list = DB::queryAll("select * from #__news order by news_id desc");
        include_once TEMPLATES_DIR.DS."news_list.php";
    }
    public function editop(){
        if(isset($_POST['submit_news_edit'])){
            $news_id = $_POST['news_id'];
            $update_array = array();
            $update_array['news_title']= $_POST['news_title'];
            $update_array['news_contents'] = $_POST['news_contents'];
            $cache_file_obj = new Cache_file('news');
            if($cache_file_obj->updateRecordAndFile('news',$update_array,"news_id='$news_id'",$news_id,'news_detail_tpl.php')){
                $this->showMessage('编辑该新闻及静态文件成功',self::NEWS_LIST_URL,1);
            }else{
                echo $cache_file_obj->error;
            }

        }else{
            $news_id = $_GET['news_id'];
            $row_news = DB::queryRow("select * from #__news where news_id='$news_id'");
            include_once TEMPLATES_DIR.DS."news_edit.php";
        }
    }
    public function delop(){
        $news_id = $_GET['news_id'];
        $cache_file_obj = new Cache_file('news');
       if($cache_file_obj->delRecordAndFile('news',"news_id='$news_id'",$news_id)){
           $this->showMessage('删除该新闻及静态文件成功',self::NEWS_LIST_URL,1);
       }else{
           echo $cache_file_obj ->error;
       }
    }
    public function del_moreop(){
        $id_array = $_POST['del'];
        $ids = implode(',',$id_array);
        $cache_file_obj = new Cache_file('news');
        if($cache_file_obj->delMoreRecordsAndFiles('news',"news_id in ($ids)",$id_array)){
            $this->showMessage('删除多条新闻及静态文件成功',self::NEWS_LIST_URL,1);
        }else{
            echo $cache_file_obj ->error;
        }
    }
    public function list_staticop(){
        $cache_file_obj = new Cache_file('news');
        $sql = "select * from #__news order by news_id desc";
        $cache_file_obj->addCacheList($sql,'news_list_tpl.php');
    }

}